<?php
namespace app\common\model;

use think\Model;

class Menu extends Model
{
    public function insert($data = array()){
        if(!$data || !is_array($data)) {
            return 0;
        }
        return $this->insertGetId($data);
    }

    public function getMenus($data) {
        $data['status'] = array('neq', -1);
        $order = [
            'listorder' => 'desc',
            'menu_id' => 'desc',
        ];

        $result = $this->where($data)
             ->order($order)
             ->paginate(3);
        return $result;
    }

    public function find($id) {
        if (!$id || !is_numeric($id)) {
            return array();
        }
        $data = [
            'menu_id' => $id
        ];
        return $this->where($data)
            ->find();
    }

    public function updateMenuById($id, $data){
        if (!$id || !is_numeric($id)) {
            throw_exceptions('ID不合法');
        }
        if (!$data || !is_array($data)) {
            throw_exceptions('更新的数据不合法');
        }
        return $this->where('menu_id',$id)->update($data);
    }

    public function updateStatusById($id, $status){
        if(!is_numeric($id) || !$id) {
            throw_exception("ID不合法");
        }
        if(!is_numeric($status) || !$status) {
            throw_exception("状态不合法");
        }

        $data['status'] = $status;
        return $this->where('menu_id',$id)->update($data);
    }

    public function updateMenuListorderById($id, $listorder){
        if (!$id || !is_numeric($id)) {
            throw_exception('ID不合法');
        }

        $data = array(
            'listorder' => intval($listorder)
        );
        return $this->where('menu_id', $id)->update($data);
    }

    public function getAdminMenus(){
        $data = [
            'status' => array('neq',-1),
            'type' => 1
        ];
        return $this->where($data)->order('listorder desc, menu_id desc')->select();
    }

    public function getBarMenus() {
        $data = array(
            'status' => array('neq',-1),
            'type' => 0
        );
        return $this->where($data)->order('listorder desc, menu_id desc')->select();
    }

}